package com.jiyuren.spring.core;

import java.util.concurrent.ExecutionException;

/**
 * Created by Administrator on 2017/3/21.
 */
public class RunThread extends Thread {
    public final static int STOP = -1;
    public final static int SUSPEND = 0;
    public final static int RUNNING = 1;
    private int status = STOP;
    private long count = 0;


    private static volatile RunThread mInstance;

    public static RunThread getInstance() {
        if (null == mInstance) {
            synchronized (RunThread.class) {
                if (null == mInstance) {
                    mInstance = new RunThread();
                }
            }
        }
        return mInstance;
    }


    public RunThread() {
    }


    @Override
    public synchronized void run() {
        // 判断是否停止
        while (status != STOP) {
            // 判断是否挂起
            if (status == SUSPEND) {
                try {
                    // 若线程挂起则阻塞自己
                    wait();
                } catch (InterruptedException e) {
                    System.out.println("线程异常终止...");
                }
            } else {
                count++;
                System.out.println(this.getName() + "第" + count + "次运行...");
                if (null != mRunListener) {
                    mRunListener.onRunning();
                }
            }
        }
    }

    @Override
    public synchronized void start() {
        status = RUNNING;
        super.start();
    }

    /**
     * 恢复
     */
    public synchronized void myResume() {
        System.out.println("恢复线程");
        // 修改状态
        status = RUNNING;
        // 唤醒
        notifyAll();
    }

    /**
     * 挂起
     */
    public void mySuspend() {
        // 修改状态
        System.out.println("挂起线程");
        status = SUSPEND;
    }

    /**
     * 停止
     */
    public void myStop() {
        // 修改状态
        System.out.println("停止线程");
        status = STOP;
    }


    private RunListener mRunListener;

    public void setRunListener(RunListener l) {
        mRunListener = l;
    }

    public interface RunListener {
        void onRunning() ;
    }

    public boolean isRunning() {
        return status == RUNNING;
    }

    public int getStatus() {
        return status;
    }

    public static void main(String[] args) {
        RunThread demo = RunThread.getInstance();
        demo.start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            System.out.println("主线程异常终止...");
        }
        System.out.println("测试线程即将被挂起...");
        demo.mySuspend();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            System.out.println("主线程异常终止...");
        }
        System.out.println("测试线程即将被唤醒...");
        demo.myResume();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            System.out.println("主线程异常终止...");
        }
        System.out.println("终止测试线程...");
        demo.myStop();
        System.out.println("主线程终止...");
    }

}
